import axios from 'axios'

const service = axios.create({
    baseURL: 'http://localhost:8000',  // 后端API地址
    timeout: 5000
})

// 请求拦截器
service.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token')
        if (token) {
            config.headers['Token'] = token
        }
        config.headers['Trace-Id'] = 'trace-' + Date.now() + '-' + Math.random().toString(36).substr(2, 9)
        config.headers['Context-Type'] = 'application/json'
        return config
    },
    error => {
        console.error(error)
        return Promise.reject(error)
    }
)

// 响应拦截器
service.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        console.error('请求错误:', error)
        return Promise.reject(error)
    }
)

export default service